<?php
/**
 * SpecialForm class.
 * SpecialForm is the data structure for keeping merchant special offer data.
 * It is used by the 'form', 'preview' action of 'MerchantController'.
 * 
 * Yawii 版权所有 @ 2012
 */
class SpecialForm extends CActiveRecord
{
	public $uts_start_date;
	public $uts_end_date;
	
	/**
	 * Return html string with weekdays.
	 * @param string $weekdays 1,2,3 | 1,2,3,4,5 | 3,4 | 5
	 */
	public static function HtmlWeekdays($weekdays, $predefined_days) {
		$array = explode(',', $weekdays);
		
		//only one day here
		if (count($array) == 1) {
			if (isset($predefined_days[$array[0]])) {
				return $predefined_days[$array[0]];
			} else {
				return null;
			}
		}
		
		//check if there is no break day
		$isbreak = false;
		$begin = $array[0];
		foreach ($array as $day) {
			if ($begin != $day) {
				$isbreak = true;
				break;
			}
			$begin ++;
		}
		$selected_days = null;
		if ($isbreak) {
			foreach ($array as $day) {
				if (isset($predefined_days[$day])) $selected_days .= "{$predefined_days[$day]}<br/>";
			}
		} else {
			$selected_days = $predefined_days[$array[0]] . ' - ' . $predefined_days[$array[count($array)-1]];
		}
		
		return $selected_days;
	}
	
	/**
	 * Returns the static model of the specified AR class.
	 * @return ShopProfile the static model class
	 */
	public static function model($className = __CLASS__) {
		return parent::model($className);
	}

	/**
	 * @return string the associated database table name
	 */
	public function tableName() {
		return 'tbl_merchant_special';
	}

	/**
	 * Declares the validation rules.
	 */
	public function rules() {
		return array(
			// fields are required
			array('value, content', 'required'),
			//array('discount', 'required'),
			array('url_picture, type, max_number', 'safe'),
			array('price, value, discount, show_price, content, week_days, start_hour, end_hour, start_date, end_date', 'unsafe'),
		);
	}

	/**
	 * Declares attribute labels.
	 */
	public function attributeLabels() {
		return array(
			'content' => BUtils::T('special_content_lbl'),
			'price' => BUtils::T('special_price_lbl'),
			'value' => BUtils::T('special_value_lbl'),
			'discount' => BUtils::T('discount_lbl'),
			'name' => BUtils::T('company_name_lbl'),
			'max_number' => BUtils::T('special_max_number_lbl'),
		);
	}
	
	protected function afterFind() {
		parent::afterFind();
		$this->uts_start_date = strtotime($this->start_date);
		$this->uts_end_date = strtotime($this->end_date);
	}
}
